package com.ccx.coachsite.ui.own;

import java.util.List;
import java.util.Map;

import com.ccx.coachsite.R;
import com.ccx.coachsite.bus.CommonBus;
import com.ccx.coachsite.bus.MessageInfo;
import com.ccx.coachsite.http.BaseResp;
import com.ccx.coachsite.util.StringUtil;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class OwerMessageView  extends OwerBaseView{

	private ListView mMessageList;
	private OwerMessageListAdapter mAdapter;
	private List<MessageInfo> mApp;
	private View mEditPanel;
	private boolean isEdit;
	private View btnDelete;
	public OwerMessageView(Context context, OwerMainBase superView) {
		
		super(context, superView);
		mEditPanel = findViewById(R.id.ower_message_edit_panel);
		isEdit = false;
		mEditPanel.setVisibility(View.GONE);
		btnDelete = findViewById(R.id.login_top_right);
		//SetEditInternal(false);
		mMessageList = (ListView) findViewById(R.id.ower_message_list);
		View btnSelectAll = findViewById(R.id.ower_message_btn_select_all);
		View btnDeleteMessage = findViewById(R.id.ower_message_btn_delete);
		
		loadData();
		btnSelectAll.setOnClickListener(new OnSelectAllListener());
		btnDeleteMessage.setOnClickListener(new OnDeleteMessageListener());
	}
	
	class OnSelectAllListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			if(mAdapter != null)
			{
				mAdapter.doSelectAll(true);
			}
		}
	}
	
	class OnDeleteMessageListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			
			String messages = getDeleMessages();
			
			if(!StringUtil.isEmpty(messages))
			{
				deleteMessageBus dmb = new deleteMessageBus(mContext);
				dmb.setDeleteMessageParam(messages);
				dmb.doTask();
			}
			else
			{
				Toast.makeText(mContext, "您还未选择任何待删除消息", Toast.LENGTH_SHORT).show();
			}
		}
	}
	
	private String getDeleMessages()
	{
		String message = "";
		Map<Integer, Boolean> list = mAdapter.getSelectedMap();
		for(int i = mApp.size() - 1; i>= 0; i--)
		{
			if(list.get(i))
			{
				message += mApp.get(i).getId()+",";
			}
		}
		if(message.length() > 0)
			message = message.substring(0, message.length() - 1);
		return message;
	}
	
	protected boolean onLoadData() 
	{
		mApp = OwerProvider.getInstance().getMessageList(mContext);
		if (mApp == null)
			return false;
		return true;
	}
	
	protected void doRefreshDataSuccess() {
		mMessageList.setOnItemClickListener(new OnItemClickListener());
		mMessageList.setDivider(null);
		
		mAdapter = new OwerMessageListAdapter(mContext, mApp);
		mMessageList.setAdapter(mAdapter);
	}
	
	private void SetEditInternal(boolean edit)
	{
		isEdit = edit;
		btnDelete.setSelected(isEdit);
		if(isEdit)
		{
			if(mAdapter != null)
			{
				mAdapter.resetList(mApp);
			}
			mEditPanel.setVisibility(View.VISIBLE);
		}
		else
		{
			mEditPanel.setVisibility(View.GONE);
		}
		
		mAdapter.setEdit(isEdit);
		mAdapter.notifyDataSetChanged();
	}
	
	public void setEditState()
	{
		if(isBusy)
		{
			Toast.makeText(mContext, "数据正在加载中，请稍后重试", Toast.LENGTH_SHORT).show();
			return;
		}
		
		SetEditInternal(!isEdit);
	}
	
	private void doDeleteMessage()
	{
		Map<Integer, Boolean> list = mAdapter.getSelectedMap();
		
		for(int i = mApp.size() - 1; i>= 0; i--)
		{
			if(list.get(i))
			{
				mApp.remove(i);
			}
		}
		
		mAdapter.resetList(mApp);
		
		if(mApp.size() == 0)
		{
			SetEditInternal(false);
		}
	}
	
	private class OnItemClickListener implements android.widget.AdapterView.OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> adapter, View v, int pos, long arg3) {
			if(pos < mApp.size())
			{
				SwitchView(OwerViewKey.ll_personal_message_detail, mApp.get(pos));
				//mSuperView.SwitchMessageDetail(OwerViewKey.ll_personal_message_detail, mApp.get(pos));
			}
		}
	}
	
	class deleteMessageBus extends CommonBus{

		public deleteMessageBus(Context context) {
			super(context);
		}
		
		@Override
		public void callBack(int taskType, BaseResp response, String responseJson) {
			super.callBack(taskType, response, responseJson);
			
			if(response.isSuccess())
			{
				doDeleteMessage();
			}
			else
			{

			}
		}
	}
}